 import getStartEndDate from '../../core/util/getStartEndDate'; 

function queryAssessmentPeaceCtrl($scope, $http, ngDialog) {
  'ngInject';

  this.orgId = $state.params.orgId;
  this.bidId = $state.params.bidId;

  var myDate = new Date();
  this.years = myDate.getFullYear();
  this.month = myDate.getMonth();

  let startEndDate = getStartEndDate(this.years, this.month);

  this.queryData = {
    checkCompletedTimeFrom: startEndDate.startDateStr,
    checkCompletedTimeTo: startEndDate.endDateStr,
  };

  if (this.orgId) {
    this.queryData.orgId = this.orgId;
  } else if (this.bidId) {
    this.queryData.bidId = this.bidId;
  }

  this.assessmentPeaceList = {};

  // 分页
  this.paginationData = {
    pageNum: 0,
    pageSize: 10,
    total: ''
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {

    this.paginationData1 = {
      pageNum: this.paginationData.pageNum-1,
      pageSize: 10,
      total: '',
    }

    $http.post(`${SERVER_CONTEXT}/api/securityActivities/app/querySpecialCheckByBid`,
      angular.extend(this.queryData, this.paginationData1)
        )
      .then((resp) => {
        this.assessmentPeaceList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 分页查询方法
  this.paginationQuery = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/app/querySpecialCheckByBid`,
      angular.extend(this.queryData, this.paginationData)
        )
      .then((resp) => {
        if(resp.statusText == 'OK'){
          this.assessmentPeaceList = resp.data.data;
          this.paginationData.total = resp.data.total;
        }        
    });

  };

  // status == 3 显示考核得分,其他情况显示进行中
  this.getTotalScore = (param) => {
    switch(param.status) {
      case 3:
        return param.totalScore;
        break;
      default:
        return '进行中';
    }
  }

  // 查询
  this.query = (param) => {
    
    if(param.endTime.$viewValue && param.startTime.$viewValue){
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.paginationQuery();
    } else {
        layer.alert('巡查开始时间应小于结束时间');
      }
    } else {
      this.paginationQuery();
    }

  } 

  // 重置
  this.reset = () => {
    this.queryData = {
      bidId: this.bidId, // 不确定是哪个标段id
      orgId: this.orgId,
    };
  }

  // 删除平安考核列表 
  this.deleteAssessmentPeace = (id) => {
    $scope.modalTitle = '删除平安考核';
    $scope.modalContent = '确认要删除该信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
      then((value) => {
        $http.delete(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/deleteSpecialCheckById/${id}`).
          then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $state.reload();
            } else {
              layer.alert(resp.data.message);
            }
          });
      }, (reason) => {});
  }

  // 升序、降序(字段暂时未给)
  this.up = (param,index) => {   
    this.queryData.columeName = param;
    this.queryData.order = index;
    this.paginationQuery();
  }

  this.initPage = () => {
    // 获取巡查列表分页
    this.paginationQuery();
  }

  // 初始化
  this.initPage();

}


export default queryAssessmentPeaceCtrl;